package handler

import (
	"course-hub/user-api/basic/pkg"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"strconv"
)

func SendSms(c *gin.Context) {

	tel := c.Query("tel")
	code := rand.Intn(9000) + 1000
	sms, err := pkg.SendSms(strconv.Itoa(code), tel)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg":  "发送失败",
		})
		return
	}
	if *sms.Body.Code != "OK" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 0,
			"msg":  "发送失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 500,
		"msg":  "发送成功",
		"data": code,
	})
}
